package lab9;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.Semaphore;

public class HttpServer extends Thread
{
	private ServerSocket socket;
	private int maxConnections = 50;
	private Semaphore sem;
	
	public HttpServer() throws IOException
	{
		socket = new ServerSocket(80);
		sem = new Semaphore(maxConnections);
	}
	
	void connectionReleased ()
	{
		sem.release();
		System.out.println("closed a connection");
	}
	
	public void run ()
	{
		while (true)
		{
			try
			{
				sem.acquire(1);
				Socket connection = socket.accept();
				RequestWorker worker = new RequestWorker(connection, this);
				worker.run();
			} 
			catch (IOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InterruptedException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
